package com.bootcamp.javase2.threads;

/**
 * @program: ThreadCode
 * @description: make thread execute in order
 * @author: Keyu Li
 * @create: 2021-09-13 21:02
 **/

public class ThreadSequel {
    public static void main(String[] args) {
        OrderThread t1 = new OrderThread("0", 0);
        OrderThread t2 = new OrderThread("1", 1);
        OrderThread t3 = new OrderThread("2", 2);

        OrderThread.setFlags(0);
        t1.start();
        t2.start();
        t3.start();

    }
}

class OrderThread extends Thread {
    static boolean[] flags = new boolean[3];
    int order;

    public OrderThread(String name, int order) {
        super(name);
        this.order = order;
    }

    public static void setFlags(int i){
        flags[i] = true;
    }

    @Override
    public void run() {
        while (!flags[order]) {

        }
        printLoop();
        if (order == 0) flags[1] = true;
        else if (order == 1) flags[2] = true;
    }

    public void printLoop(){
        System.out.println(getName()+" print: ");
        for (int i = 0; i < 15; i++) {
            System.out.print(i+", ");
        }
        System.out.println();
    }
}